﻿using EasyAdmin.Application.Contracts.Systems.Roles;
using EasyAdmin.Application.Contracts.Systems.Roles.Dtos;
using EasyAdmin.Domain.Entities.Systems;
using EasyAdmin.Web.Authorizations;
using EasyCore.UnifyResult;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace EasyAdmin.Web.Host.Controllers
{
    /// <summary>
    /// 角色管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class RolesController : ControllerBase
    {
        private readonly IRoleAppService _roleAppService;
        public RolesController(IRoleAppService roleAppService) {
            _roleAppService = roleAppService;
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <returns></returns>
       
        [HttpGet]
        [AppAuthorize("abc")]
        public async Task<dynamic> Get([FromQuery]RolePageCondition condition) {
            return await _roleAppService.Page(condition);     
        }

        /// <summary>
        /// 获得所有角色
        /// </summary>
        /// <returns></returns>
        [HttpGet("all-roles")]
        public async Task<dynamic> GetAllRoles() {
            return await _roleAppService.GetAllRoles();
        }

        /// <summary>
        /// 改变角色状态
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet("change/{roleId}")]
        public async Task ChangeRoleStatusByRoleId([FromRoute] long roleId)
        {
            await _roleAppService.ChangeRoleStatusByRoleId(roleId);
        }

        /// <summary>
        /// 插入角色信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task Post([FromBody]CreateUpdateRoleDto input) {
            await _roleAppService.CreateRole(input);
        }

        /// <summary>
        /// 修改角色信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut]
        async Task Put([FromBody]CreateUpdateRoleDto input)
        {
            await _roleAppService.UpdateRole(input);
        }

        /// <summary>
        /// 删除角色信息
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpDelete("roleId")]
        public async Task Delete([FromRoute]long roleId) { 
        await _roleAppService.DeleteRoleById(roleId);
        }

        /// <summary>
        /// 获得角色详细信息
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet("{roleId}")]
        public async Task<RoleOutput> Get([FromRoute]long roleId) { 
            return await _roleAppService.GetRoleByRoleId(roleId);
        }
    }
}
